package Cursus.Listeners;

import javax.swing.*;
import java.io.*;

public class FileListModel extends DefaultListModel {
    FileReader fr;
    BufferedReader br;
    String line;
    FileWriter fw;
    BufferedWriter bw;

    public FileListModel(String filename) {
        try {
            fr = new FileReader(filename);
            br = new BufferedReader(fr);
            line = br.readLine();
            while (line != null) {
                super.addElement(line);
                line = br.readLine();
            }
        } catch (Exception ee) {}
    }

    public void saveListModelToFile(String filename) {
        try {
            fw = new FileWriter(filename);
            bw = new BufferedWriter(fw);
            int aantal = super.getSize();
            for(int i = 0; i < aantal; i++) {
                line = (String)super.getElementAt(i);
                bw.write(line,0,line.length());
                bw.newLine();
            }
            bw.close();
        } catch (Exception e) {}
    }
}
